package com.lanou.service.impl;

import com.lanou.entity.*;
import com.lanou.mapper.TbItemCatMapper;
import com.lanou.mapper.TbItemMapper;
import com.lanou.mapper.TbItemParamItemMapper;
import com.lanou.mapper.TbItemParamMapper;
import com.lanou.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by QuChao on 2018/11/8
 */
@Service
public class ItemParamServiceImpl implements ItemParamService {
    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    private Integer updateId = null;

    @Override
    public List<Object> findAll() {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        tbItemParamExample.createCriteria();
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);
        List<Object> list = new ArrayList<>();
        for (TbItemParam tbItemParam : tbItemParamList) {
            TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(tbItemParam.getItemCatId());
            ItemParm itemParm = new ItemParm();
            itemParm.setId(tbItemParam.getId());
            itemParm.setItemCatId(tbItemParam.getItemCatId());
            itemParm.setItemCatName(tbItemCat.getName());
            itemParm.setParamData(tbItemParam.getParamData());
            itemParm.setCreated(tbItemParam.getCreated());
            itemParm.setUpdated(tbItemParam.getUpdated());
            list.add(itemParm);
        }
        return list;
    }

    @Override
    @Transactional
    public boolean deleteByIds(String[] ids) {
        for (String id : ids) {
            int i = tbItemParamMapper.deleteByPrimaryKey(Long.parseLong(id));
            if (i == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Map<String, Object> findByItemCatId(Integer id) {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        tbItemParamExample.createCriteria().andItemCatIdEqualTo(id.longValue());
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);
        Map<String, Object> map = new HashMap<>();
        if (tbItemParamList.size() == 0) {
            map.put("status", 400);
        } else {
            map.put("status", 200);
            map.put("data", tbItemParamList.get(0));
        }
        return map;
    }

    @Override
    public Map<String, Object> save(Integer id, TbItemParam tbItemParam) {
        Date date = new Date();
        tbItemParam.setCreated(date);
        tbItemParam.setUpdated(date);
        tbItemParam.setId(null);
        tbItemParam.setItemCatId(id.longValue());
        System.out.println(tbItemParam);
        int insert = tbItemParamMapper.insert(tbItemParam);
        Map<String, Object> map = new HashMap<>();
        if (insert == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 400);
        }
        return map;
    }

    @Override
    public Map<String, Object> findByItemCatIdResultAll(Long id) {

        TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
        tbItemParamItemExample.createCriteria().andItemIdEqualTo(id);
        List<TbItemParamItem> tbItemParamItemList = tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamItemExample);


        Map<String, Object> map = new HashMap<>();
        if (tbItemParamItemList.size() == 1) {
            map.put("status", 200);
            map.put("data", tbItemParamItemList.get(0));
        } else {
            map.put("status", 400);
        }
        return map;
    }

}
